//
//  BCDebugDeviceConfig.m
//  BCDebugKit
//
//  Created by Basic on 2018/3/26.
//

#import "BCDebugDeviceConfig.h"
#import "BCDebugUtils.h"
#import "BCDebugCom.h"
#import "BCDebugKitPublic.h"
#import "BCDebugKitProtocol.h"

#define kBCDebugDeviceConfig_ENV   @"debug_env"

@interface BCDebugDeviceConfig()<UIAlertViewDelegate, BCDebugKitProtocol>
@end


@implementation BCDebugDeviceConfig

//MARK: - 环境配置
static NSInteger kDefaultDebugEnv = 0;
+(NSInteger)defaultDebugEnv {
    return kDefaultDebugEnv;
}
+ (void)setDefaultDebugEnv:(NSInteger)defaultDebugEnv {
    kDefaultDebugEnv = defaultDebugEnv;
}
+ (NSInteger )currentENV {
    NSInteger currentENV = 0;//默认线上
#ifdef DEBUG
    currentENV = kDefaultDebugEnv;//debug，默认test环境
    NSNumber *envNumber = [[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugDeviceConfig_ENV];
    if (envNumber) {
        currentENV = [envNumber integerValue];
    }
#endif
    return currentENV;
}
+ (NSString *)currentENVString {
    NSInteger currentENV = [[self class] currentENV];
    NSString *strENV = currentENV == 0 ? @"线上":@"测试";
    return strENV;
}

///MARK: -  BCDebugKitProtocol
-(BCDebugItemGroup *)bcDebugConfigSetupDebugGroup {
    if ([self.configDelegate respondsToSelector:@selector(bcDebugConfigSetupDebugGroup)]) {
        return [self.configDelegate bcDebugConfigSetupDebugGroup];
    }
    //app版本
    NSString *appVer = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString *mode = nil;
#ifdef DEBUG
    mode = @"debug";
#else
    mode = @"release";
#endif
    
    BCDebugItemGroup *debugGroup = [BCDebugItemGroup new];
    debugGroup.bcName = @"设备信息";
    debugGroup.bcConfigClass = [self class];
    //系统版本
    NSString *isJail = [BCDebugUtils deviceJailBroken]?@"已越狱":@"未越狱";
    BCDebugItemModel *debugItem = [BCDebugItemModel new];
    debugItem.bcName = @"系统信息";
    debugItem.bcDetail = [NSString stringWithFormat:@"iOS%@,%@,v%@,%@,%@",[UIDevice currentDevice].systemVersion, isJail, appVer, build, mode];
    debugItem.bcSelectStyle = UITableViewCellSelectionStyleNone;
    [debugGroup.bcItems addObject:debugItem];

    //当前环境
    NSInteger currentEnv = [[self class] currentENV];
    BCDebugItemModel *itemSwitch = [BCDebugItemModel new];
    itemSwitch.bcName = @"服务器地址";
    itemSwitch.bcDetail = [[self class] currentENVString];
    itemSwitch.bcSelectOptionIndex = (currentEnv>0)?(currentEnv - 1):0;
    itemSwitch.bcType = BCDebugItemType_Selector;
    itemSwitch.bcAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    __weak typeof(self) weakself = self;
    itemSwitch.selectOptionAction = ^(BCDebugItemModel *item, BCDebugItemOption *option, NSInteger selectRow){
        //保存上次选择的索引和 环境值
        [[NSUserDefaults standardUserDefaults] setValue:@(option.bcTag) forKey:kBCDebugDeviceConfig_ENV];
        [[NSUserDefaults standardUserDefaults] synchronize];
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIAlertView *alertTip = [[UIAlertView alloc] initWithTitle:nil message:@"切换成功，重启APP才能生效哦" delegate:weakself cancelButtonTitle:@"关闭App" otherButtonTitles:nil];
        alertTip.delegate = self;
        [alertTip show];
    #pragma clang diagnostic pop
    };
    BCDebugItemOption *debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"线上环境";
    debugOption1.bcTag = 0;
    [itemSwitch.bcOptions addObject:debugOption1];
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"测试环境";
    debugOption1.bcTag = 1;
    [itemSwitch.bcOptions addObject:debugOption1];

    [debugGroup.bcItems addObject:itemSwitch];
    
    return debugGroup;
}

///MARK: -  privete
- (void)sendUmDeviceId:(NSString *)deviceid {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = deviceid;

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"已拷贝Id,请前往APP粘贴" message:deviceid preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *_Nonnull action) {
    }];
    UIAlertAction *wxAction = [UIAlertAction actionWithTitle:@"微信" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *_Nonnull action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
#pragma clang diagnostic pop
        
    }];
    UIAlertAction *ddAction = [UIAlertAction actionWithTitle:@"钉钉" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *_Nonnull action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"dingtalk://"]];
#pragma clang diagnostic pop
    }];
    [alertVC addAction:wxAction];
    [alertVC addAction:ddAction];
    [alertVC addAction:cancelAction];
    [[BCDebugCom sharedInstance].rootNavcDebug presentViewController:alertVC animated:YES completion:NULL];
}

#pragma mark - UIAlertViewDelegate
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
//#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0){
//        exit(0);
        abort();
//        NSAssert(0, @"BCDebugKit Switch Env,and kill app");
    }
}
//#endif
#pragma clang diagnostic pop

@end
